<template>
  <el-table :data="PermisstionArr" style="width: 100%; margin-bottom: 20px" row-key="id" border>
    <el-table-column label="名称" prop="name"></el-table-column>
    <el-table-column label="权限值" prop="code"></el-table-column>
    <el-table-column label="修改时间" prop="updateTime"></el-table-column>
    <el-table-column label="操作">
      <!-- row:即为已有的菜单对象|按钮的对象的数据 -->
      <template #="{ row }">
        <el-button type="primary" @click="addPermisstion(row)" size="small" :disabled="row.level == 4 ? true : false">{{ row.level == 3 ? '添加功能' : '添加菜单' }}</el-button>
        <el-button type="primary" @click="updatePermisstion(row)" size="small" :disabled="row.level == 1 ? true : false">编辑</el-button>
        <el-popconfirm :title="`你确定要删除${row.name}?`" width="260px" @confirm="removeMenu(row.id)">
          <template #reference>
            <el-button type="primary" size="small" :disabled="row.level == 1 ? true : false">删除</el-button>
          </template>
        </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!-- 添加|编辑按钮的对话框 -->
  <el-dialog v-model="dialogVisible" title="添加菜单">
    <el-form>
      <el-form-item label="名称">
        <el-input placeholder="请填写名称" v-model="menuData.name"></el-input>
      </el-form-item>
      <el-form-item label="权限">
        <el-input placeholder="请填写权限值" v-model="menuData.code"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save"> 确定 </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, onMounted, reactive } from 'vue'
//引入获取菜单请求API
import { reqAllPermisstion, reqAddOrUpdateMenu ,reqRemoveMenu} from '@/api/acl/menu/index.ts'
//引入ts类型
import type { PermisstionResponseData, PermisstionList, Permisstion } from '@/api/acl/menu/type'
import { ElMessage } from 'element-plus'
//存储菜单的数据
let PermisstionArr = ref<PermisstionList>([])

//组件挂载完毕
onMounted(() => {
  getHasPermisstion()
})
//获取菜单数据的方法
const getHasPermisstion = async () => {
  let result: PermisstionResponseData = await reqAllPermisstion()
  if (result.code == 200) {
    PermisstionArr.value = result.data
  }
}

//添加|编辑按钮的对话框，默认隐藏
let dialogVisible = ref<boolean>(false)
//收集对话框的数据
let menuData = reactive({
  code: '', //权限数值
  level: 0, //几级菜单
  name: '', //菜单的名字
  pid: 0 //菜单的ID
})
//添加按钮
let addPermisstion = (row: Permisstion) => {
  //lever值
  menuData.level = row.level + 1
  //pid
  menuData.pid = row.id as number
  //清空数据
  Object.assign(menuData, {
    code: '',
    level: 0,
    name: '',
    pid: 0,
    id: 0
  })
  //对话框打开
  dialogVisible.value = true
}
//编辑按钮
let updatePermisstion = (row: Permisstion) => {
  //点击修改按钮:收集已有的菜单的数据进行更新
  Object.assign(menuData, row)
  dialogVisible.value = true
}

//对话框确定按钮
let save = async () => {
  let res: any = await reqAddOrUpdateMenu(menuData)
  if (res.code == 200) {
    ElMessage({ type: 'success', message: '添加成功' })
    //再次获取全部最新的菜单的数据
    getHasPermisstion()
    //对话框隐藏
    dialogVisible.value = false
  }
}
//删除菜单按钮回调
let removeMenu =async (id:number)=>{
  let res:any = await reqRemoveMenu(id)
  if(res.code==200){
    ElMessage({type:'success',message:'删除成功'})
    getHasPermisstion()
  }
}
</script>

<style scoped></style>
